import prompt from '@system.prompt';
import router from '@system.router';
import dataStorage from '@ohos.data.storage'
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
import featureAbility from '@ohos.ability.featureAbility';
import http from '@ohos.net.http';
import request from '@ohos.request';

export default {
    data: {
        title: 'World',
        lable: 'uploadvideopage',
        videoname:null,
        videoAbsolutePath:null,
        userid:'-1',
        context:null,
        path:null,
        cachepath:null,
        event:'',
        seekingtime:'',
        timeupdatetime:'',
        seekedtime:'',
        isStart: true,
        isfullscreenchange: false,
        duration: '',
        videodocument:null,
        uploadvideostatus:0,
        videoSrc:'',
    },
    async onShow(){
        this.context = featureAbility.getContext()
        this.path = await this.context.getFilesDir()
        this.cachepath = await this.context.getCacheDir()
    },
    TurnBack(){
        router.back();
    },
    ChooseVideo(){
        var that = this
        let option = {type : "video", count : 1  };
        mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
            console.log("Media resources selected."+value);             // Obtain the media selection value.
            // 使用calculate同步方法，返回OperateData对象。
            this.utilInterface = createLocalParticleAbility('edu.zut.hys.jsskateboard.util.UtilServiceAbility');
            this.utilInterface.CopyToCache(value[0],result => {
                console.info(this.lable+result)
                that.videoname = result;
                that.videoAbsolutePath = "internal://cache/"+result;//"file://"+that.cachepath+'/'+result
                prompt.showToast({
                    message: '选择成功'
                })
                that.UploadVideo();
            })
        }).catch((err) => {
            console.log("An error occurred when selecting media resources.");
            prompt.showToast({
                message: '选择失败'
            })
        });
    },
    UploadVideo(){
        var that = this
        let storage = dataStorage.getStorageSync(this.path + '/mystore')
        this.userid = storage.getSync('userid', '-1')
        if(this.userid === '-1'){
            prompt.showToast({
                message: '请先登录'
            })
            return;
        }
        if(this.videoname!==null)
        request.upload({ url: 'http://101.35.82.76:9001/upload',
            files:[{
                       filename: this.videoname,
                       name:'file',
                       uri:'internal://cache/'+this.videoname,
                       type:'mp4',
                   }],
            data:[{name:"userid",value:this.userid}]
        },
            (err, data) => {
                if (err) {
                    prompt.showToast({
                        message: '上传失败'
                    })
                    console.error('Failed to request the upload. Cause: ' + JSON.stringify(err));
                    return;
                }
                that.uploadvideostatus = 1;
                prompt.showToast({
                    message: '上传成功'
                })
            });
    },
    preparedCallback:function(e){ this.event = '视频连接成功'; this.duration = e.duration;},
    startCallback:function(){ this.event = '视频开始播放';},
    pauseCallback:function(){ this.event = '视频暂停播放'; },
    finishCallback:function(){ this.event = '视频播放结束';},
    errorCallback:function(){ this.event = '视频播放错误';},
    seekingCallback:function(e){ this.seekingtime = e.currenttime; },
    timeupdateCallback:function(e){ this.timeupdatetime = e.currenttime;},
    change_start_pause: function() {
        if(this.isStart) {
            this.$element('videoId').pause();
            this.isStart = false;
        } else {
            this.$element('videoId').start();
            this.isStart = true;
        }
    },
    change_fullscreenchange: function() {//全屏
        if(!this.isfullscreenchange) {
            this.$element('videoId').requestFullscreen({ screenOrientation : 'default' });
            this.isfullscreenchange = true;
        } else {
            this.$element('videoId').exitFullscreen();
            this.isfullscreenchange = false;
        }
    },
    videoDocument(msg){
        this.videodocument = msg.text;
    },
    UploadDocument(){
        var that = this
        if(this.uploadvideostatus === 0){
            prompt.showToast({
                message: '请先选择视频'
            })
            return;
        }
        let httpRequest= http.createHttp();
        httpRequest.request("http://101.35.82.76:9002/file/addDocument?userid="+this.userid+'&document='+this.videodocument,
            {
                method: 'GET',
                readTimeout: 60000,
                connectTimeout: 60000
            },(err, data) => {
                if (!err) {
                    console.info('Result:' + data.result);
                    console.info('code:' + data.responseCode);
                    console.info('header:' + data.header);
                    prompt.showToast({
                        message: '发布成功'
                    })
                    var res = JSON.stringify(JSON.parse(data.result).data)
                    var videoSrc = JSON.stringify(JSON.parse(res).videoSrc)
                    this.videoSrc = JSON.parse(videoSrc)

                } else {
                    console.info('error:' + err.data);
                    prompt.showToast({
                        message: 'uploaddocument Send Error'+ JSON.stringify(err)
                    })
                }
            });
    },
}
